package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.GoodsOrderMapper;
import com.hd.pzhwlxt.mapper.TransMapper;
import com.hd.pzhwlxt.pojo.GoodsOrder;
import com.hd.pzhwlxt.pojo.GoodsPrice;
import com.hd.pzhwlxt.pojo.Trans;
import com.hd.pzhwlxt.service.GoodsOrderService;
import com.hd.pzhwlxt.service.GoodsPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @create: 2022-09-26 9:03
 * @author: 怀念天空
 * @Description:
 */

@Service
public class GoodsOrderServiceImpl extends ServiceImpl<GoodsOrderMapper, GoodsOrder> implements GoodsOrderService {

    @Autowired
    private GoodsPriceService goodsPriceService;

    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @Autowired
    private TransMapper transMapper;


    @Override
    public boolean addGoodsOrder(GoodsOrder goodsOrder) {
        //计算价格
        Integer goodsId = goodsOrder.getGoodsId();
        QueryWrapper<GoodsPrice> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId);
        GoodsPrice goodsPrice = goodsPriceService.getOne(wrapper);
        Double outPrice = goodsPrice.getOutPrice();
        goodsOrder.setMoney(BigDecimal.valueOf(outPrice * goodsOrder.getNum()));
        //订单里已经有记录了，但是运输管理模块是还没有记录的。
        int insert = goodsOrderMapper.insert(goodsOrder);
        Trans trans = new Trans();
        trans.setOrderId(goodsOrder.getId());
        //将订单添加到管理模块
        transMapper.insert(trans);
        return insert > 0;
    }

    @Override
    public List<GoodsOrder> getAllGoodsOrder() {
        return goodsOrderMapper.getAllGoodsOrder();
    }

    @Override
    public Page<GoodsOrder> getAllGoodsOrderPage(Page<GoodsOrder> page) {
        return goodsOrderMapper.getAllGoodsOrderPage(page);
    }

    @Override
    public List<GoodsOrder> getGoodsOrderToDo() {
        return goodsOrderMapper.getAllGoodsOrderToDo();
    }

    @Override
    public Page<GoodsOrder> getGoodsOrderToDoPage(Page<GoodsOrder> page) {
        return goodsOrderMapper.getGoodsOrderToDoPage(page);
    }

    @Override
    public List<GoodsOrder> getGoodsOrderOnDo() {
        return goodsOrderMapper.getAllGoodsOrderOnDo();
    }

    @Override
    public Page<GoodsOrder> getGoodsOrderOnDoPage(Page<GoodsOrder> page) {
        return goodsOrderMapper.getGoodsOrderOnDoPage(page);
    }
}

